var isApprove= "approve" == getUrlParam("act");
var approvalId = getUrlParam("id");
//var serverPre1 = "http://172.30.13.92:9001"
var serverPre1 ='http://10.150.30.54'
serverPre1 = serverPre

    function initTableByData() {
    var approvalDetail = getApprovalDetail();

    if (approvalDetail == null) {
        layer.msg("未找到该审批流数据！", {icon: 0,time: 2000}, function () {
            closeLayer(false)
        });
        return
    }
    if (isApprove && (approvalDetail.approvalStatus == 0 || approvalDetail.approvalStatus == 1)) {
        $(".approve_show_class").show();
    }

    approvalDetail.approvalWayName=approvalDetail.approvalWay==1?"邮件":"审批流";
    $("#approve_table [aim_key]").each(function(){
        var content=approvalDetail[$(this).attr("aim_key")];

        if(isBlank(content)){
            content=$(this).attr("empty_default");
        }

        if(isNotBlank(content)){
            $(this).html(content)
        }
    })

    var enclosureId=approvalDetail.enclosureId;
    var enclosureName=approvalDetail.enclosureName;

    if(approvalDetail.approvalWay==1){
        $("#approve_enclosure").remove();

        $("#approve_email img").attr("src",enclosureId)
    }else{
        $("#approve_email").remove();

        if(isNotBlank(enclosureId)){
            var enclosure= $("#approve_enclosure a")
            enclosure.attr("href",approvalDetail.enclosureUrl)
            enclosure.html(isNotBlank(enclosureName)?enclosureName:"附件");
        }else {
            $("#approve_enclosure td:last").html("无")
        }
    }
    $("#approve_approval td:last").load("approvalView.html","",function () {
        initViewApprover(initApproversByData(approvalDetail))
    })
}

function initApproversByData(approvalDetail) {
    var approversConfig = {
        status: approvalDetail["approvalStatus"],
        approvers: [{name: approvalDetail["applyUserName"], status: 1, extras: []}]
    };

    approvalDetail.approvers.forEach(function (e) {
        approversConfig.approvers.push({
            name: e.approverName, status: e.approvalStatus, extras:
                e.approvalStatus != 0 && e.approvalStatus != -1 ? [{name: "审批时间", value: e.updateTime}, {
                    name: "审批意见",
                    value: isNotBlank(e.description) ? e.description : "无"
                }] : []
        });
    });
    return approversConfig;
}


function getApprovalDetail() {
    var data = null;
    debugger;
    $.ajax({
        type: 'get',
        url: serverPre1 + "/manager/saas/v1/approval/detail/" + approvalId,
        headers: {"token": getCookie("token")},
        dataType: 'json',
        async: false,
        cache: false,
        success: function (result) {
            if (result.code == 0 && result.data) {
                data = result.data
            }
        }
    });
    return data;
}

function closeLayer(ifReloadData){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
    if(ifReloadData){
        parent.location.reload();
    }
}

$(function () {
    layui.use(['form'], function(){
        var form = layui.form;
        form.render();
    });

    if("true"!=getUrlParam("hideCancelBtn")){
        $("#cancelApproveBtn").click(function () {
            closeLayer(false)
        })
    }else {
        $("#cancelApproveBtn").hide()
    }

    $("#toApproveBtn").click(function () {
        var ifConfirm=false;
        layer.confirm('确定提交审批?', {btn: ['确定', '取消']}, function (index, layero) {
            layer.closeAll('dialog');
            var approveData = {"id": approvalId,"approvalStatus": $("input[name='approvalStatus']:checked").val(),"description": $("#approve_description").val()}
            $.ajax({
                type: 'post',
                url: serverPre1 + "/manager/saas/v1/approval/approve",
                dataType: 'json',
                headers: {"token": getCookie("token"),"Content-Type":"application/json;charset=UTF-8"},
                async: false,
                cache: false,
                data: JSON.stringify(approveData),
                success: function (result) {
                    if (result.code == 0) {
                        layer.msg("审批成功！", {icon: 1,time: 2000}, function () {
                            closeLayer(true)
                        });
                    } else {
                        layer.msg(result.msg, {icon: 1,time: 2000});
                    }
                }
            });
         });
    })
})

